package seatiger.tomcat.catalina.core;

import seatiger.tomcat.catalina.*;
import seatiger.tomcat.catalina.connector.NewRequest;
import seatiger.tomcat.catalina.connector.NewResponse;
import seatiger.tomcat.util.StringManager;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * StandardEngine基础阀
 */
public class StandardEngineValve extends ValveBase {

    private static final String info =
            "seatiger.tomcat.catalina.core.StandardEngineValve/1.0";

    private static final StringManager sm =
            StringManager.getManager(Constants.PACKAGE_CORE);

    @Override
    public String getInfo() {
        return info;
    }

    @Override
    @Deprecated
    public void invoke(Request request, Response response, ValveContext context) throws IOException, ServletException {
        StandardEngine engine = (StandardEngine) getContainer();
        //根据映射获取到对应的host
        Host host = (Host) engine.map(request, true);
        if (host == null) {
            ((HttpServletResponse) response.getResponse()).sendError
                    (HttpServletResponse.SC_BAD_REQUEST,
                            sm.getString("standardEngine.noHost",
                                    request.getRequest().getServerName()));
            return;
        }
        host.invoke(request, response);
    }

    @Override
    public void invoke(NewRequest request, NewResponse response) throws IOException, ServletException{
        Host host = request.getHost();
        host.getPipeline().getFirst().invoke(request,response);
    }
}
